/***********************
*DESC:命令配置
*Author:wff 
*Date:2015-05-12
*
************************/
function GetCurrcTypeOemIdModelData(curcmdctype, curoemid)
{
	curoemid = '00';
	var isINsert = false;
	if(InitMcmdArry.length == 0){
			
		return false;
	}
	else{
		
		$.each(InitMcmdArry.param,function(index,item){
			if(item.ctype == curcmdctype){
				var oemidarry;
				oemidarry = item.oem;
				if(typeof(oemidarry) == "object" && oemidarry.constructor == Array){
					$.each(oemidarry,function(indexoem,itemoem){
						if(itemoem.oemId == curoemid){
							var modelarry;
							modelarry = itemoem.model;
							if(typeof(modelarry) == "object" && modelarry.constructor == Array){
								cmdOemIdModelMap = new Map();
								$.each(modelarry,function(indexmodel,itemmodel){
									cmdOemIdModelMap.put(itemmodel.modelId,itemmodel.modelName);	
									
									isINsert = true;
								})
							}else{
								isINsert = true;
								cmdOemIdModelMap = new Map();
								cmdOemIdModelMap.put(itemoem.model.modelId,itemoem.model.modelName);
							}
							
							return false;
						}
						
					})
				}
				else{
					if(item.oem.oemId == curoemid){
						var modelarry;
							modelarry = item.oem.model;
							if(typeof(modelarry) == "object" && modelarry.constructor == Array){
								cmdOemIdModelMap = new Map();
								$.each(modelarry,function(indexmodel,itemmodel){
									cmdOemIdModelMap.put(itemmodel.modelId,itemmodel.modelName);	
									
									isINsert = true;
								})
							}else{
								isINsert = true;
								cmdOemIdModelMap = new Map();
								cmdOemIdModelMap.put(item.oem.model.modelId,item.oem.model.modelName);
							}
							
							return false;
					}
				}
				
				return false;
			}				
		})
						
		return isINsert;		
	}
	
}
function GetCMDTableResult(gridId)
{
	var data=[];
	data = $.fn.tables.GetCMDTableResult(gridId);
	return data;
}
/*function IsFindCmdData(curepid)
{
	var isFind = false;
	$.each(CurrElemCMDArry,function(index,item) {
		if (curepid == item.epid) {
			isFind = true;
		}
	});
	return isFind;
}*/
function InitOidSelect()
{
	$('#CMDAddtable #oidSelect').empty();
	$('#CMDAddtable #oidSelect').append("<option value='' selected='selected' onchange='CCMDOidChange(this)'></option>");
	$.each(sceneTypeMap.arr,function(index,item) {
		$('#CMDAddtable #oidSelect').append("<option value='"+item.key+"' onchange='CCMDOidChange(this)'>"+item.value+"</option>");		
	});
}
function InitCMDFormData(curepid)
{
	var isINsert = false;
	var isFind   = false;
	$('#cmdctype').removeAttr("disabled");
	$('#cmdrw').removeAttr("disabled");
	$('#MCMDtitle').removeAttr("hidden");
	$('#MCMDtable').removeAttr("hidden");
	$firstRow = $('#CMDAddtable tbody').find('tr:first-child');
	$firstRow.find('#id').removeAttr("disabled");
	$firstRow.find('#dtype').removeAttr("disabled");
	$('#CMDAddtable #oidSelect').hide();
	$('#CMDAddtable #oid').show();
	//if (IsFindCmdData(curepid) == false) {
		$.each(CurrElemArry,function(index,item) {
			if (curepid == item.epid) {
				if (item.etype == "01") {
					$('#cmdctype').val(0);
					$('#cmdctype').attr("disabled","disabled");
					$('#cmdrw').val(2);
					$('#cmdrw').attr("disabled","disabled");
					$('#MCMDtitle').attr("hidden","hidden");
					$('#MCMDtable').attr("hidden","hidden");
					$firstRow = $('#CMDAddtable tbody').find('tr:first-child');
					$firstRow.find('#id').attr("disabled","disabled");
					$firstRow.find('#dtype').attr("disabled","disabled");
					InitOidSelect();
					$('#CMDAddtable #oidSelect').show();
					$('#CMDAddtable #oid').hide();
				}
			}
		});
	//}
	if(CurrElemCMDArry.length == 0){
		InitCCMDForm();
		InitMCMDForm('2');
		return false;
	}
	else{
		
		$.each(CurrElemCMDArry,function(index,item){
			if(item.epid == curepid){
				isFind   = true;
				isINsert = true;								
				Curctype = item.ctype;
				Currw = item.rw;
				$('#cmdctype').val(Curctype);
				$("#cmdctype option[value='" + Curctype + "']").attr("selected","selected");
//				if (typeof(Curctype) == 'undefined') {
//					$.each(CurrElemArry,function(index,item) {
//						if (curepid == item.epid) {
//								$('#cmdctype').val(0);
//								InitOidSelect();
//								$('#CMDAddtable #oidSelect').show();
//								$('#CMDAddtable #oid').hide();
//								InitCCMDForm();
//						}
//					});
//				}
				$('#cmdrw').val(Currw);
				$("#cmdrw option[value='" + Currw + "']").attr("selected","selected");
				if(item.ctype == 0){
					InitMCMDForm(Currw);
				}else{
					if(item.hasOwnProperty("mcmd")){
						if(item.mcmd.i == 0){							
							InitMCMDForm(Currw);
						}else{
							var mcmdi = item.mcmd.i;
							if(typeof(mcmdi) == "object" && mcmdi.constructor == Array){
								//$.fn.tables.bindObjData('MCMDtable', eval(item.mcmd.i));
								mcmdi.sort(
									function(a, b)  
									{  
										var aa = parseFloat(a.id);
										var bb = parseFloat(b.id);
										 
										if(aa < bb) 
											return -1;  
										else if(aa > bb) 
											return 1;  
										else 
											return 0;  
									}  
								);
								$.fn.tables.bindObjData('MCMDtable', eval(mcmdi));
							}
							else{
								var NewI = [];
								NewI.push(mcmdi);
								$.fn.tables.bindObjData('MCMDtable', eval(NewI));
							}
						}
					}
					else
						InitMCMDForm(Currw);
				
				}
				if(item.hasOwnProperty("ccmd")){
										
					if(item.ccmd.i == 0){							
								InitCCMDForm();
					}else{
							var ccmdi = item.ccmd.i;
							if(typeof(ccmdi) == "undefined") {
								return;
							}
							if(typeof(ccmdi) == "object" && ccmdi.constructor == Array){
								//$.fn.tables.bindObjData('CMDAddtable', eval(item.ccmd.i));
								ccmdi.sort(
									function(a, b)  
									{  
										var aa = parseFloat(a.id);
										var bb = parseFloat(b.id);
										 
										if(aa < bb) 
											return -1;  
										else if(aa > bb) 
											return 1;  
										else 
											return 0;  
									}  
								);
								$.fn.tables.bindObjData('CMDAddtable', eval(ccmdi));
							}else{
								var NewI = [];
								NewI.push(ccmdi);
								$.fn.tables.bindObjData('CMDAddtable', eval(NewI));
							}
					}
				}
				else
					InitCCMDForm();
				
				return false;			
			}
			if(isINsert == true){
				
			return false;
			}
		})
		if (isFind == false) {
	    	$.each(CurrElemArry,function(index,item) {
				if (curepid == item.epid) {
					if (item.etype == "01") {
						$('#cmdctype').val(0);
						InitOidSelect();
						$('#CMDAddtable #oidSelect').show();
						$('#CMDAddtable #oid').hide();
						InitCCMDForm();
					}
				}
			});
	    }	
		if(isINsert == false){
			InitCCMDForm();
			InitMCMDForm('2');
			return false;
		}
	    
	}
	if ($('#cmdctype').val() != '0') {
		$('#CMDAddtable #oidSelect').hide();
		$('#CMDAddtable #oid').show();
	}	
}
function GetCurrcmdTypeMCMDData(curcmdctype, curoemid, curmodel)
{
	curoemid = '00';
	curmodel = '00';
	if(curmodel == '' || curmodel == undefined)
		curmodel = '00';
	var isINsert = false;
	if(InitMcmdArry.length == 0){
			
		return false;
	}
	else{
		var ers;
		$.each(InitMcmdArry.param,function(index,item){
			if(item.ctype == curcmdctype){
				var oemidarry;
				oemidarry = item.oem;
				if(typeof(oemidarry) == "object" && oemidarry.constructor == Array){
					$.each(oemidarry,function(indexoem,itemoem){
						if(itemoem.oemId == curoemid){
							var modelarry;
							modelarry = itemoem.model;
							if(typeof(modelarry) == "object" && modelarry.constructor == Array){
								
								$.each(modelarry,function(indexmodel,itemmodel){
									if(itemmodel.modelId == curmodel){
										isINsert = true;
										ers = itemmodel.cmd;
										return false;
									}
								})
								
							}else{
								if(itemoem.model.modelId == curmodel){					  
									
										isINsert = true;
										ers = itemoem.model.cmd;										
								}
								
							}
							
							return false;
						}
						
					})
				}
				else{
					
					if(item.oem.oemId == curoemid){
						var modelarry;
							modelarry = item.oem.model;
							if(typeof(modelarry) == "object" && modelarry.constructor == Array){
								
								$.each(modelarry,function(indexmodel,itemmodel){
									if(itemmodel.modelId == curmodel){
										isINsert = true;
										ers = itemmodel.cmd;
										return false;
									}
								})
								
							}else{
								if(item.oem.model.modelId == curmodel){					  
									
										isINsert = true;
										ers = item.oem.model.cmd;										
								}
								
							}
							
							return false;
						
					}
				}
				
				return false;
			}				
		})
						
		if(isINsert == false){
				
			return false;
		}
		else{
				
			return ers;
		}		
	
		
	}
	
}
function InitMCMDForm(Crw)
{
		$('#cmdctype').val('0');
		$("#cmdctype option[value='0']").attr("selected","selected");
		
		$('#cmdrw').val(Crw);
		$("#cmdrw option[value='" + Crw + "']").attr("selected","selected");

		var trHtml = $("#MCMDtable").find("tr").slice(1).first().html();
		$("#MCMDtable").find("tbody").find("tr").remove();
		$("#MCMDtable").append("<tr>" + trHtml + "</tr>");
		
}
function InitCCMDForm()
{
		
		var trHtml = $("#CMDAddtable").find("tr").slice(1).first().html();
		$("#CMDAddtable").find("tbody").find("tr").remove();
		$("#CMDAddtable").append("<tr>" + trHtml + "</tr>");
}
function GetCMDFrom(curepid, curoemid)
{
	$('#CMDAddModal .input-small').removeClass("error");
	$('#CMDAddModal .text-error').remove();
	$('#oemId').val(curoemid);
	$("#cmddtypeTR").hide();
	$('#CMDAddModal').modal('show');
	$("#CMDAddModal_addbtn").attr("onclick","AddCMDAction('"+curepid+"');");
	    nowepid = curepid;
		InitCMDFormData(curepid);
}

function AddCMDAction(curepid)
{
	 
	  var cmdctype = $('#cmdctype').val();
	  var cmdrw = $('#cmdrw').val();
	  if(cmdctype == '0'){
	  	var ccmdTab = GetCMDTableResult('CMDAddtable');
		if(ccmdTab != 0){
			var ispass = validateRow('CMDAddtable');
			if(!ispass){
				$.formSmallInit();
				
				return;
			}
		}
	  }
	  else{
		  var ccmdTab = GetCMDTableResult('CMDAddtable');
		  var mcmdTab = GetCMDTableResult('MCMDtable');
		  var ismcmdTabpass = true;
		  var isccmdTabpass = true;
		  if(mcmdTab != 0){
			ismcmdTabpass = validateRow('MCMDtable');
			
		 }
		 if(ccmdTab != 0){
			isccmdTabpass = validateRow('CMDAddtable');			
		 }
		 if(ismcmdTabpass == false || isccmdTabpass == false)
		 {
			 $.formSmallInit();
				
				return;
		 }
	  }
	 /*已有epid cmd对象中先删除*/
	 if(CurrElemCMDArry.length != 0){	 		  
		  $.each(CurrElemCMDArry,function(index,item){
			if(item.epid == curepid){
				
				CurrElemCMDArry.splice(index, 1);
				return false;
			}				
		 })
		 // CurrElemCMDArry.push({"epid": curepid,"ctype": cmdctype,"mcmd":{"i": GetCMDTableResult('MCMDtable')},"ccmd":{"i": GetCMDTableResult('CMDAddtable')}});
	  }
	  if(cmdctype == '0'){
		 var ir = '0';
		
		 CurrElemCMDArry.push({"epid": curepid,"ctype": cmdctype,"rw": cmdrw,"mcmd":{"i": ir},"ccmd":{"i": GetCMDTableResult('CMDAddtable')}});
		 	
	  }
	  else
	 	 CurrElemCMDArry.push({"epid": curepid,"ctype": cmdctype,"rw": cmdrw,"mcmd":{"i": GetCMDTableResult('MCMDtable')},"ccmd":{"i": GetCMDTableResult('CMDAddtable')}});
	 $('#CMDAddModal').modal('hide');
	
}

function CCMDOidChange(obj)
{
    var $raw = $(obj).closest('tr');
    var $oid = $raw.find('#oid');
    var $dtype = $raw.find('#dtype');
    $oid.val($(obj).val());
    $dtype.val('6');
    /*******刷新自定义命令表中id使其自增********/
    var rows = $('#CMDAddtable tbody').children();
	for (var i=0; i<rows.length; i++) {
		$(rows[i]).find('#id').val(i+1);		
	}
}

//删除【自定义命令】行
function deleteCcmdRow(obj) {
		$.fn.tables.deleteRow(obj);
		var rows = $('#CMDAddtable tbody').children();
		for (var i=0; i<rows.length; i++) {
			$(rows[i]).find('#id').val(i+1);		
		}
		if (rows.length == 1) {
			if ($(rows[0]).find('#name').val().length==0 && $(rows[0]).find('#oid').val().length==0 && $(rows[0]).find('#dtype').val().length==0 && $(rows[0]).find('#val').val().length==0) {
				$(rows[0]).find('#id').val("");
			}
		}
}

